package com.toptech.downloadmanager.ToolUtils;

import com.toptech.downloadmanager.TaskInfo;

import org.litepal.crud.DataSupport;

import java.util.List;

/**
 * Created by zhengshaorui on 2017/3/9.
 */

public class LitePalUtils {
    private static final String TAG = "LitePalUtils";

    /**
     * 查询任务数据库是否存在
     * @param url
     * @return
     */
    public static boolean isTaskInfoExsits(String url){


        List<TaskInfo> taskInfos = DataSupport.where("fileurl = ?",url).find(TaskInfo.class);
        if (taskInfos.isEmpty()){
            return  false;
        }else{
            return true;
        }

    }

    /**
     * 获取单挑数据的进度值
     * @param url
     * @return
     */

    public static int getTaskProgress(String url){
        List<TaskInfo> taskInfos = DataSupport.where("fileurl = ?",url).find(TaskInfo.class);
        if (taskInfos.isEmpty()){
            return  0;
        }else{
            TaskInfo taskinfo = taskInfos.get(0);
            return taskinfo.getFileprogress();
        }
    }

    /**
     * 获取单条数据的id
     * @param url
     * @return
     */

    public static long getTaskId (String url){

        List<TaskInfo> taskInfos = DataSupport.where("fileurl = ?",url).find(TaskInfo.class);
        if (!taskInfos.isEmpty()){
            return  taskInfos.get(0).getId();
        }
        return 0;
    }
}
